import user from '@/sheep/store/user.js'
import $store from '@/sheep/store';
import {
	showAuthModal,
	showShareModal
} from '@/sheep/hooks/useModal';
import {
	isNumber,
	isString,
	isEmpty,
	startsWith,
	isObject,
	isNil,
	clone
} from 'lodash';
import throttle from '@/sheep/helper/throttle';
// 白名单
let white = [
	'/pages/user/index/index',
	'/pages/user/user/user',
	'/pages/public/auth/register',
	'/pages/public/auth/login',
	'/pages/public/auth/authorization_login',
	'/pages/public/auth/real_authentication',
	'/pages/public/auth/bind_mobile',
	'/pages/public/auth/reset_password',
	'/pages/public/richtext',
	'/pages/user/task/task',
	'/subpackage/user/index/menu_list'
	// '/subpackage/user/user/edit_data',
];
const _go = (
	path,
	params = {},
	options = {
		redirect: false,
	},
) => {
	let page = ''; // 跳转页面
	let query = ''; // 页面参数
	let url = ''; // 跳转页面完整路径
	if (isString(path)) {
		// 判断跳转类型是 path ｜ 还是http
		if (startsWith(path, 'http')) {
			// #ifdef H5
			window.location = path;
			return false;
			// #endif
		} else if (startsWith(path, 'action:')) {
			handleAction(path);
			return false;
		} else {
			[page, query] = path.split('?');
		}
		if (!isEmpty(params)) {
			let query2 = paramsToQuery(params);
			if (isEmpty(query)) {
				query = query2;
			} else {
				query += '&' + query2;
			}
		}
	}

	if (isObject(path)) {
		page = path.url;
		if (!isNil(path.params)) {
			query = paramsToQuery(path.params);
		}
	}

	const nextRoute = ROUTES_MAP[page];

	// 未找到指定跳转页面
	// mark: 跳转404页
	if (!nextRoute) {
		console.log('未找到指定跳转页面');
		return false;
	}


	if (!white.includes(nextRoute.path)) {
		if (!$store('user').isLogin) {
			showAuthModal('mpAuthorization');
			return false;
		}

		if ($store('user').isLogin) {
			if ($store('user').userInfo.is_authentication == 0) {
				showAuthModal('authentication');
				return false;
			}
		}

		if ($store('user').isLogin) {
			if (!$store('user').userInfo.mobile) {
				showAuthModal('bind_mobile');
				return false;
			}
		}
	}

	// if($store('user').stayPort == 'staff') {
	// 	$store('user').setabPort('staff');
	// 	uni.switchTab({
	// 		url: '/pages/staff/index/index',
	// 	});
	// 	return false;
	// }

	url = page;
	if (!isEmpty(query)) {
		url += `?${query}`;
	}
	// 跳转底部导航
	if (TABBAR.includes(page)) {
		uni.switchTab({
			url,
		});
		return false;
	}

	// 使用redirect跳转
	if (options.redirect) {
		uni.redirectTo({
			url,
		});
		return false;
	}

	uni.navigateTo({
		url,
	});
};

// 限流 防止重复点击跳转
function go(...args) {
	throttle(() => {
		_go(...args);
	});
}

function paramsToQuery(params) {
	if (isEmpty(params)) {
		return '';
	}
	let query = [];
	for (let key in params) {
		query.push(key + '=' + params[key]);
	}

	return query.join('&');
}

function back(return_refresh = false) {
	if(return_refresh ){
	let pages = getCurrentPages();
	let prevPage = pages[pages.length - 2];
	uni.navigateBack({
		delta: 1,
		success: function () {
			prevPage.onLoad(prevPage.options);	
		}
		});
	}else{
		// #ifdef H5
		history.back();
		// #endif
		// #ifndef H5
		uni.navigateBack();
		// #endif
	}
}

function redirect(path, params = {}) {
	go(path, params, {
		redirect: true,
	});
}

// 检测是否有浏览器历史
function hasHistory() {
	// #ifndef H5
	const pages = getCurrentPages();
	if (pages.length > 1) {
		return true;
	}
	return false;
	// #endif
}

function getCurrentRoute(field = '') {
	let currentPage = getCurrentPage();
	// #ifdef MP
	currentPage.$page['route'] = currentPage.route;
	currentPage.$page['options'] = currentPage.options;
	// #endif
	if (field !== '') {
		return currentPage.$page[field];
	} else {
		return currentPage.$page;
	}
}

function getCurrentPage() {
	let pages = getCurrentPages();
	return pages[pages.length - 1];
}

function handleAction(path) {
	const action = path.split(':');
	switch (action[1]) {
		case 'showShareModal':
			showShareModal();
			break;
	}
}

function error(errCode, errMsg = '') {
	redirect('/pages/public/error', {
		errCode,
		errMsg,
	});
}
// 更新上一页数据
function renewal() {
	
	let pages = getCurrentPages();
	let prevPage = pages[pages.length - 2];
	prevPage.onLoad(prevPage.options);	

}

export default {
	go,
	back,
	hasHistory,
	redirect,
	getCurrentPage,
	getCurrentRoute,
	error,
	renewal
};